package io.renren.service.business;

import io.renren.dto.AppCommentDto;
import io.renren.dto.ColumnAppDto;
import io.renren.entity.App;
import io.renren.entity.AppCategory;
import io.renren.entity.AppComment;
import io.renren.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author winson
 * @CREATE 11 23:56
 */
@Service("appBusinessService")
public class AppBusinessService {

    @Autowired
    private AppCategoryService appCategoryService;

    @Autowired
    private AppColumnLinkService appColumnLinkService;

    @Autowired
    private AppCategoryLinkService appCategoryLinkService;

    @Autowired
    private AppService appService;

    @Autowired
    private AppCommentService appCommentService;

    public List<AppCategory> getCategoryList(){
        return appCategoryService.queryBySort();
    }


    public List<ColumnAppDto> getColumnAppList(){
        return appColumnLinkService.getColumnAppList( 5 );
    }

    public List<App> getIndex(){
        Map<String,Object> params = new HashMap<>();
        params.put("limit", 8);
        return (List<App>) appService.queryPage( params ).getList();
    }

    public List<App> getAppByCategory( Integer page, Integer categoryId ){
        List<App> list = appCategoryLinkService.queryAppByCategory( page == null ? 1 : page, 8, categoryId );
        if( list == null ){
            list = new ArrayList<>();
        }
        return list;
    }

    public List<App> getAppByColumn( Integer page, Integer columnId ){
        return appColumnLinkService.queryAppByColumn( page == null ? 1 : page, 8, columnId );
    }

    public App getAppDetail( Integer appId ){
        return appService.queryAppDetail( appId );
    }

    public void evaluate( Integer appId, Integer userId, String content  ){
        AppComment appComment = new AppComment();
        appComment.setAppId( appId );
        appComment.setContent( content );
        appComment.setCreateTime( new Date());
        appComment.setIsDeleted( 0 );
        appCommentService.insert( appComment );
    }

    public List<AppCommentDto> getAppCommentList( Integer page, Integer appId ){
        return appCommentService.queryAppCommentList( page, 8, appId );
    }

}
